<template>
  <div class='container' ref="container">
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />
    <div class="detail" v-if="article">
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <van-image round width="1rem" height="1rem" fit="fill" :src="article.aut_photo" />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate}}</p>
        </div>
        <van-button @click='toggleFollow(article)' round size="small" type="info">
           {{article.is_followed?'已关注':'+ 关注'}}
        </van-button>
      </div>
      <div class="content" v-html="article.content"></div>
      <div class="zan">
        <van-button  @click='handleToggle(1)' round size="small" :class="{active:article.attitude===1}" plain icon="like-o">点赞</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button  @click='handleToggle(2)' round size="small" :class="{active:article.attitude===0}" plain icon="delete">不喜欢</van-button>
      </div>
    </div>
    <!-- 文章的评论 -->
    <article-comment :articleId='id'></article-comment>
  </div>
</template>
<script>
import { getArticleDetail, followArticle, unFollowArticle, likes, unlikes, dislikes, undislikes } from '@/api/article.js'
import ArticleComment from '@/components/ArticleComment.vue'

export default {
  name: 'Detail',
  props: ['id'],
  components: {
    ArticleComment
  },
  data () {
    return {
      article: null
    }
  },
  methods: {
    async handleToggle (type) {
      // 点赞和取消点赞；不喜欢和取消不喜欢
      if (type === 1) {
        // 点赞和取消点赞
        if (this.article.attitude === 1) {
          // 取消点赞
          try {
            await unlikes(this.article.art_id)
            this.article.attitude = -1
          } catch {
            this.$toast.fail('取消点赞失败')
          }
        } else {
          // 点赞
          try {
            await likes(this.article.art_id)
            this.article.attitude = 1
          } catch {
            this.$toast.fail('点赞失败')
          }
        }
      } else {
        // 不喜欢和取消不喜欢
        if (this.article.attitude === 0) {
          // 取消不喜欢
          try {
            await undislikes(this.article.art_id)
            this.article.attitude = -1
          } catch {
            this.$toast.fail('取消不喜欢失败')
          }
        } else {
          // 不喜欢
          try {
            await dislikes(this.article.art_id)
            this.article.attitude = 0
          } catch {
            this.$toast.fail('不喜欢失败')
          }
        }
      }
    },
    // 关注与取消关注文章
    async toggleFollow (article) {
      if (article.is_followed) {
        // 取消关注
        try {
          await unFollowArticle(article.aut_id)
          article.is_followed = !article.is_followed
        } catch {
          this.$toast.fail('取消关注失败')
        }
      } else {
        // 进行关注
        try {
          await followArticle(article.aut_id)
          article.is_followed = !article.is_followed
        } catch {
          this.$toast.fail('关注失败')
        }
      }
    },
    // 获取文章详情数据
    async getArticleDetail () {
      try {
        const ret = await getArticleDetail(this.id)
        this.article = ret.data
      } catch {
        this.$toast.fail('获取文章详情数据失败')
      }
    }
  },
  created () {
    this.getArticleDetail()
  }
}
</script>
<style scoped lang="less">
.container {
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
}
.detail {
  padding: 46px 10px 44px;
  // height: 1000%;
  .title {
    font-size: 18px;
    line-height: 2;
  }
  .zan{
    text-align: center;
    padding: 10px 0;
    .active{
      border-color:red;
      color: red;
    }
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.5;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    padding: 20px 0;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img{
      max-width:100%;
      background: #f9f9f9;
    }
    /deep/ code{
      white-space: pre-wrap;
    }
    /deep/ pre{
      white-space: pre-wrap;
    }
  }
}
</style>
